<?php

namespace Dddxiu\rules;

use Dddxiu\Layer;

/**
 * 日期
 */
class Date implements Rule
{
    // flag
    const F = 'date';


    /**
     * @param array $input
     * @param string $field
     * @param Layer $layer
     * @param array $args
     * @return \Closure|bool
     * @throws \Exception
     */
    public static function valid($input, $field, $layer, $args)
    {
        if (count($args) == 0) {
            $pass = strtotime($input[$field]) > 0;
        } else {
            $pass = (date($args[0], strtotime($input[$field])) === $input[$field]);
        }

        if ($pass) {
            return $layer::next(['type' => Layer::FIELD_TYPE_DATE]);
        }
        return false;
    }
}